package com.lyrcsoft;

import java.io.IOException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;

/**
 * 功能描述
 *
 * @author fujc-dev@qq.com
 * @date: 2023年11月13日 14:09
 */

public class RestTemplateTest {
    public static void main(String[] args) {
        ClientHttpRequestInterceptor i1 = new ClientHttpRequestInterceptor() {
            @Override
            public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                System.out.println("拦截器1开始。。。");
                ClientHttpResponse execute = execution.execute(request, body);
                System.out.println("拦截器1结束。。。");
                return execute;
            }
        };
        ClientHttpRequestInterceptor i2 = new ClientHttpRequestInterceptor() {
            @Override
            public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                System.out.println("拦截器2开始。。。");
                ClientHttpResponse execute = execution.execute(request, body);
                System.out.println("拦截器2结束。。。");
                return execute;
            }
        };
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(i1);
        restTemplate.getInterceptors().add(i2);
        String forObject = restTemplate.getForObject("http://www.baidu.com", String.class);
        System.out.println(forObject);
    }
}
